{smcl}
{com}{sf}{ul off}{txt}{.-}
      name:  {res}<unnamed>
       {txt}log:  {res}C:\Users\thies\OneDrive\00_Promotion\00_Output\00_Paper\2022_Predicting_Econ_Sanctions\Empirics\20231214_Replication\Log_files/01c_Onset_limited_sample_US.smcl
  {txt}log type:  {res}smcl
 {txt}opened on:  {res}18 Dec 2023, 16:18:18
{txt}
{com}. 
. ***************************************************************
. ***US***
. ***************************************************************
. 
. set seed 1234
{txt}
{com}. 
. *Prepare data
. use "Dataset.dta", clear
{txt}
{com}. keep if sender=="US"
{txt}(10,154 observations deleted)

{com}. 
. * Independent Variables
. gen ln_oil_gas_value_2014 = ln(oil_gas_value_2014+1)
{txt}(681 missing values generated)

{com}. gen sender_colony=US_colony
{txt}(16 missing values generated)

{com}. gen sender_trade = ln_US_Trade_COW
{txt}(448 missing values generated)

{com}. gen coup_dummy = coup1
{txt}(5 missing values generated)

{com}. replace coup_dummy = 0 if coup_dummy == 1
{txt}(61 real changes made)

{com}. replace coup_dummy = 1 if coup_dummy == 2
{txt}(45 real changes made)

{com}. 
. * Dependent variable : 1 if a threat or sanction case started in the dyad
. xtset ccodecow year
{res}
{col 1}{txt:Panel variable: }{res:ccodecow}{txt: (unbalanced)}
{p 1 16 2}{txt:Time variable: }{res:year}{txt:, }{res:{bind:1989}}{txt: to }{res:{bind:2015}}{p_end}
{txt}{col 10}Delta: {res}1 unit
{txt}
{com}. replace caseid=0 if caseid==.
{txt}(3,884 real changes made)

{com}. gen sanctiononset = (caseid-l.caseid)
{txt}(199 missing values generated)

{com}. replace sanctiononset=1 if sanctiononset > 1 & !missing(sanctiononset)
{txt}(209 real changes made)

{com}. replace sanctiononset=0 if sanctiononset < 0
{txt}(153 real changes made)

{com}. replace sanctiononset=. if sanctiononset == 0 & (sanction_dyad == 1 | threat_dyad == 1)
{txt}(963 real changes made, 963 to missing)

{com}. tab sanctiononset

{txt}sanctionons {c |}
         et {c |}      Freq.     Percent        Cum.
{hline 12}{c +}{hline 35}
          0 {c |}{res}      3,706       94.66       94.66
{txt}          1 {c |}{res}        209        5.34      100.00
{txt}{hline 12}{c +}{hline 35}
      Total {c |}{res}      3,915      100.00
{txt}
{com}. gen sanction_test = sanctiononset if year > 2009
{txt}(4,058 missing values generated)

{com}. 
. * lag time-series variables
. sort ccodecow year
{txt}
{com}. by ccodecow: gen l_v2x_polyarchy = v2x_polyarchy[_n-1] if year==year[_n-1]+1
{txt}(803 missing values generated)

{com}. by ccodecow: gen l_gd_ptss = gd_ptss[_n-1] if year==year[_n-1]+1
{txt}(579 missing values generated)

{com}. by ccodecow: gen l_coup_dummy = coup_dummy[_n-1] if year==year[_n-1]+1
{txt}(203 missing values generated)

{com}. by ccodecow: gen l_one_sided_violence = one_sided_violence[_n-1] if year==year[_n-1]+1
{txt}(199 missing values generated)

{com}. by ccodecow: gen l_conflict = conflict[_n-1] if year==year[_n-1]+1
{txt}(199 missing values generated)

{com}. by ccodecow: gen l_mid_terr_integrity = mid_terr_integrity[_n-1] if year==year[_n-1]+1
{txt}(199 missing values generated)

{com}. by ccodecow: gen l_ln_GDPpc_imputed = ln_GDPpc_imputed[_n-1] if year==year[_n-1]+1
{txt}(318 missing values generated)

{com}. by ccodecow: gen l_sender_trade = sender_trade[_n-1] if year==year[_n-1]+1
{txt}(451 missing values generated)

{com}. by ccodecow: gen l_ln_oil_gas_value = ln_oil_gas_value_2014[_n-1] if year==year[_n-1]+1
{txt}(686 missing values generated)

{com}. by ccodecow: gen l_defense_alliance = defense_alliance[_n-1] if year==year[_n-1]+1
{txt}(199 missing values generated)

{com}. 
. * create dummy variables
. tabulate l_gd_ptss, generate (pol_terr)

  {txt}l_gd_ptss {c |}      Freq.     Percent        Cum.
{hline 12}{c +}{hline 35}
          1 {c |}{res}      1,130       25.12       25.12
{txt}          2 {c |}{res}      1,263       28.08       53.20
{txt}          3 {c |}{res}      1,212       26.95       80.15
{txt}          4 {c |}{res}        635       14.12       94.26
{txt}          5 {c |}{res}        258        5.74      100.00
{txt}{hline 12}{c +}{hline 35}
      Total {c |}{res}      4,498      100.00
{txt}
{com}. 
. ** Filter for cases of importance
. keep if pot_sanctioned_countries == 1 & !missing(sanctiononset)
{txt}(2,234 observations deleted)

{com}. 
. * sortieren nach Jahr, zur Vorbereitung RF model
. gen u=0
{txt}
{com}. replace u=1 if year >= 2009
{txt}(824 real changes made)

{com}. sort u
{txt}
{com}. 
. ** Imposition
. * Random Forest Model
. rforest sanctiononset l_v2x_polyarchy pol_terr* l_coup_dummy ///
> l_one_sided_violence l_conflict l_mid_terr_integrity ///
> l_ln_GDPpc_imputed l_sender_trade l_ln_oil_gas_value ///
> sender_colony l_defense_alliance in 1/2019, type(class) iter(1500) numvars(15)
{txt}
{com}. 
. * Variable Importance
. ereturn list

{txt}scalars:
       e(Observations) =  {res}2019
           {txt}e(features) =  {res}15
         {txt}e(Iterations) =  {res}1500
          {txt}e(OOB_Error) =  {res}.070827142149579

{txt}macros:
                e(cmd) : "{res}rforest{txt}"
            e(predict) : "{res}randomforest_predict{txt}"
             e(depvar) : "{res}sanctiononset{txt}"
         e(model_type) : "{res}random forest classification{txt}"

matrices:
         e(importance) : {res} 15 x 1
{txt}
{com}. matrix list e(importance)
{res}
{txt}e(importance)[15,1]
              Variable I~e
l_v2x_poly~y {res}    .77819429
{txt}   pol_terr1 {res}    .32485669
{txt}   pol_terr2 {res}    .72844353
{txt}   pol_terr3 {res}    .83360452
{txt}   pol_terr4 {res}    .83587379
{txt}   pol_terr5 {res}     .5159174
{txt}l_coup_dummy {res}    .62974744
{txt}l_one_side~e {res}    .46874185
{txt}  l_conflict {res}    .59384022
{txt}l_mid_terr~y {res}            1
{txt}l_ln_GDPpc~d {res}     .6830804
{txt}l_sender_t~e {res}    .61307949
{txt}l_ln_oil_g~e {res}    .54857123
{txt}sender_col~y {res}     .3583725
{txt}l_defense_~e {res}    .49414087
{reset}
{com}. * write Variable importance to excel file
. putexcel set "Supplemental_Material\Variable_Importance\Variable_Importance_Onset_US_RF.xlsx", sheet("M") replace
{res}{txt}Note: File will be replaced when the first {cmd:putexcel} command is issued.

{com}. putexcel A1=matrix(e(importance)), names
{res}{txt}file {bf:Supplemental_Material\Variable_Importance\Variable_Importance_Onset_US_RF.xlsx} saved

{com}. 
. * Predictions
. predict randonsUS
{txt}
{com}. predict randonsUS0 randonsUS1, pr
{txt}
{com}. 
. * Confusion Matrix
. * Sensitivity 20, Specificity 95.76
. diagtest sanction_test randonsUS

{txt}sanction_t {c |}   predicted classes
       est {c |}         0          1 {c |}     Total
{hline 11}{c +}{hline 22}{c +}{hline 10}
         0 {c |}{res}       678          8 {txt}{c |}{res}       686 
{txt}         1 {c |}{res}        30          2 {txt}{c |}{res}        32 
{txt}{hline 11}{c +}{hline 22}{c +}{hline 10}
     Total {c |}{res}       708         10 {txt}{c |}{res}       718 

{txt}True D defined as randonsUS ~= 0                      [95% Conf. Inter.]
-------------------------------------------------------------------------
Sensitivity                     Pr( +| D)  {res}20.00%      17.07%   22.93%
{txt}Specificity                     Pr( -|~D)  {res}95.76%      94.29%   97.24%
{txt}Positive predictive value       Pr( D| +)  {res} 6.25%       4.48%    8.02%
{txt}Negative predictive value       Pr(~D| -)  {res}98.83%      98.05%   99.62%
{txt}-------------------------------------------------------------------------
Prevalence                      Pr(D)      {res} 1.39%       0.54%    2.25%
{txt}-------------------------------------------------------------------------

{com}. tab2xl sanction_test randonsUS using Supplemental_Material\Prediction_Output\Confusion_Matrixes\US_Onset_RF, row(1) col(1)
{res}{txt}file {bf:Supplemental_Material\Prediction_Output\Confusion_Matrixes\US_Onset_RF.xlsx} saved

{com}. 
. * Kappa .08
. kap sanction_test randonsUS

{txt}{col 14}Expected
Agreement   agreement     Kappa   Std. err.         Z      Prob>Z
{hline 65}
{res}  94.71%      94.27%     0.0756     0.0315       2.40      0.0082
{txt}
{com}. 
. * AUPR .08
. prtab sanction_test randonsUS1 
{txt}(5 missing values generated)

{col 12}Number of observations       =  {res}718
{txt}{col 12}Unique values of classifier  =  {res}691
{txt}{col 12}Number of positive cases     =  {res}32
{txt}{col 12}Portion of positive cases    ={res}  0.0446

{txt}{hline 50}
{col 5} Recall =  0.1250{col 25}  0.2188{col 37}  0.3125
{hline 50}
{res}{col 2}Precision{col 14}  0.1250{col 25}  0.1321{col 37}  0.0714
{txt}{hline 50}
{res}
{txt}{col 2}Area under precision-recall curve:  0.0800

{com}. graph save "Graph" "Supplemental_Material\Prediction_Output\Roc-curves\AUPR_Onset_RF_US.gph", replace
{txt}{p 0 4 2}
(file {bf}
Supplemental_Material\Prediction_Output\Roc-curves\AUPR_Onset_RF_US.gph{rm}
not found)
{p_end}
{res}{txt}file {bf:Supplemental_Material\Prediction_Output\Roc-curves\AUPR_Onset_RF_US.gph} saved

{com}. 
. log close
      {txt}name:  {res}<unnamed>
       {txt}log:  {res}C:\Users\thies\OneDrive\00_Promotion\00_Output\00_Paper\2022_Predicting_Econ_Sanctions\Empirics\20231214_Replication\Log_files/01c_Onset_limited_sample_US.smcl
  {txt}log type:  {res}smcl
 {txt}closed on:  {res}18 Dec 2023, 16:18:46
{txt}{.-}
{smcl}
{txt}{sf}{ul off}